
import unittest
import diabind
import testdata
import os

class SymFindTest( unittest.TestCase ):

    def __get_glob(self):
        if not hasattr(self, "__glob"):
            self.__glob = testdata.loaded_session.get_globalScope()
        return self.__glob

    def test_findChildren_SymTagNull(self):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagNull,
                                                    "diabind::CException::~CException",
                                                    diabind.nsfCaseSensitive )
        tags = [sym.get_symTag() for sym in enum_syms]
        self.assertTrue(diabind.SymTagFunction in tags)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagNull,
                                                    "dIAbind::g_comiNitializer",
                                                    diabind.nsfCaseInsensitive )
        tags = [sym.get_symTag() for sym in enum_syms]
        self.assertTrue(diabind.SymTagData in tags)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagNull,
                                                    "diabi?d::CLo*er",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        tags = [sym.get_symTag() for sym in enum_syms]
        self.assertTrue(diabind.SymTagUDT in tags)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagNull,
                                                    "diabind::uN*coratef?ag",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        tags = [sym.get_symTag() for sym in enum_syms]
        self.assertTrue(diabind.SymTagEnum in tags)

    def test_findChildren_SymTagFunction(self):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabind::*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertFalse("diabind::g_ComInitializer" in names)
        self.assertTrue("diabind::CException::CException" in names)
        self.assertFalse("diabind::CLoader" in names)
        self.assertFalse("diabind::UndecorateFlag"  in names)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabind::CException::CException",
                                                    diabind.nsfCaseSensitive )
        names = [sym.get_name() for sym in enum_syms]
        self.assertEqual(len(set(names)), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabind::cexcEPtion::CExceptiON",
                                                    diabind.nsfCaseInsensitive )
        names = [sym.get_name() for sym in enum_syms]
        self.assertEqual(len(set(names)), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabind::CExcep??on::CE*ion",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertEqual(len(set(names)), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabind::cexcEP?ion::c*ception",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertEqual(len(set(names)), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabi??::CException::CExcepti*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertTrue(len(set(names)) >= 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagFunction,
                                                    "diabind::??xceptiON::cexception*",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertTrue(len(set(names)) >= 1)

    def test_findChildren_SymTagData(self):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "diabind::*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertTrue("diabind::g_ComInitializer" in names)
        self.assertFalse("diabind::CException::CException" in names)
        self.assertFalse("diabind::CLoader" in names)
        self.assertFalse("diabind::UndecorateFlag"  in names)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "diabind::g_ComInitializer",
                                                    diabind.nsfCaseSensitive )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "diABind::g_cominitIALizer",
                                                    diabind.nsfCaseInsensitive )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "diab?nd::g_Co*itializer",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "Di?bind::g_*miNitializER",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "d??bind::g_Com*itializer*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        self.assertTrue(len(enum_syms) >= 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagData,
                                                    "di*ind::g_coMin?tializER*",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        self.assertTrue(len(enum_syms) >= 1)

    def test_findChildren_SymTagPublicSymbol(self):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagPublicSymbol,
                                                    "*what@exception@std@@*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_undecoratedNameEx(diabind.UndecorateFlag_NameOnly) for sym in enum_syms]
        self.assertTrue("std::exception::what" in names)

    def test_findChildren_SymTagUDT(self):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagUDT,
                                                    "diabind::*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertFalse("diabind::g_ComInitializer" in names)
        self.assertFalse("diabind::CException::CException" in names)
        self.assertTrue("diabind::CLoader" in names)
        self.assertFalse("diabind::UndecorateFlag"  in names)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagUDT,
                                                    "diabind::CLoader",
                                                    diabind.nsfCaseSensitive )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagUDT,
                                                    "dIAbind::cloader",
                                                    diabind.nsfCaseInsensitive )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagUDT,
                                                    "dia??nd::CLo*er",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagUDT,
                                                    "D???ind::C*aDER",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        self.assertEqual(len(enum_syms), 1)

    def test_findChildren_SymTagEnum(self):
        enum_syms = self.__get_glob().findChildren( diabind.SymTagEnum,
                                                    "diabind::*",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        names = [sym.get_name() for sym in enum_syms]
        self.assertFalse("diabind::g_ComInitializer" in names)
        self.assertFalse("diabind::CException::CException" in names)
        self.assertFalse("diabind::CLoader" in names)
        self.assertTrue("diabind::UndecorateFlag"  in names)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagEnum,
                                                    "diabind::UndecorateFlag",
                                                    diabind.nsfCaseSensitive )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagEnum,
                                                    "diABind::undecoRAteFlag",
                                                    diabind.nsfCaseInsensitive )
        self.assertEqual(len(enum_syms), 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagEnum,
                                                    "diab?nd::Un*orateFlag",
                                                    diabind.nsfCaseSensitive | diabind.nsfRegularExpression )
        self.assertTrue(len(enum_syms) >= 1)

        enum_syms = self.__get_glob().findChildren( diabind.SymTagEnum,
                                                    "diABin?::uNdec*atEflag",
                                                    diabind.nsfCaseInsensitive | diabind.nsfRegularExpression )
        self.assertTrue(len(enum_syms) >= 1)
